package com.samehope.ar.controller;


import com.samehope.ar.common.CommonResult;
import com.samehope.ar.dto.ItemAddParam;
import com.samehope.ar.dto.ItemUpdateParam;
import com.samehope.ar.service.IPatrolItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 巡检项 前端控制器
 * </p>
 *
 * @author Zhang Luo
 * @since 2019-12-23
 */
@RestController
@RequestMapping("/item")
public class PatrolItemController {

    @Autowired
    private IPatrolItemService itemService;

    /**
     * 新增巡检项
     * @return
     */
    @PreAuthorize("hasRole('ROLE_PROJECT')")
    @PostMapping("/add")
    public CommonResult add(@Valid @RequestBody ItemAddParam param) {
        return CommonResult.success(itemService.add(param));
    }

    /**
     * 查询巡检项明细
     * @param itemId
     * @return
     */
    @PreAuthorize("hasRole('ROLE_PROJECT')")
    @GetMapping("/itemDetail/{itemId}")
    public CommonResult itemDetail(@PathVariable("itemId") Long itemId) {
        return CommonResult.success(itemService.itemDetail(itemId));
    }

    /**
     * 更新巡检项
     * @return
     */
    @PreAuthorize("hasRole('ROLE_PROJECT')")
    @PutMapping("/update")
    public CommonResult update(@Valid @RequestBody ItemUpdateParam param) {
        itemService.updateItem(param);
        return CommonResult.success(null);
    }

    /**
     * 删除巡检项
     * @return
     */
    @PreAuthorize("hasRole('ROLE_PROJECT')")
    @DeleteMapping("/delete/{itemId}")
    public CommonResult delete(@PathVariable("itemId") Long itemId) {
        itemService.deleteItem(itemId);
        return CommonResult.success(null);
    }

    /**
     * 巡检项树结构
     * @return
     */
    @PreAuthorize("hasRole('ROLE_PROJECT')")
    @GetMapping("/tree")
    public CommonResult tree(@RequestParam(required = false) String condition) {
        return CommonResult.success(itemService.getItemTree(condition));
    }

    /**
     * 根据点位查询巡检项树结构
     * @param pointId
     * @return
     */
    @PreAuthorize("hasRole('ROLE_PROJECT')")
    @GetMapping("/treeByPoint/{pointId}")
    public CommonResult tree(@PathVariable("pointId") Long pointId) {
        return CommonResult.success(itemService.getItemTreeByPointId(pointId));
    }

    /**
     * 巡检项下拉菜单接口
     * @return
     */
    @PreAuthorize("hasRole('PROJECT')")
    @GetMapping("/itemsForSelect")
    public CommonResult itemsForSelect() {
        return CommonResult.success(itemService.itemsForSelect());
    }
}
